<?php

namespace App\Admin\Actions\Category;

use Dcat\Admin\Grid\Tools\AbstractTool;
use Illuminate\Http\Request;

class DataExport extends AbstractTool
{
    protected $title = '导出所有数据';
    protected ?string $uri = null;
    protected $style = 'btn btn-outline-info';

    public function title()
    {
        return $this->title;
    }

    public function __construct($title = null, string $uri = null)
    {
        parent::__construct($title);
        if ($uri) {
            session(['data_export_uri' => $uri]);
            $this->uri = $uri;
        } else {
            $this->uri = session('data_export_uri');
        }
    }

    public function handle(Request $request)
    {
        if (!$this->uri) {
            $this->uri = $request->get('export_uri');
        }

        return $this->response()->download('/dataExport?uri=' . $this->uri);
    }
}
